package Java7_Concurrency_Cookbook.concurrent_custom.custom_fork_join_task;

import java.util.Date;
import java.util.concurrent.ForkJoinTask;

/**
 * @author lixiangke
 * @date 2017/11/28
 */
public abstract class MyWorkerTask extends ForkJoinTask<Void> {

    private String name;

    public MyWorkerTask(String name) {
        this.name = name;
    }

    /**
     * 用来获取任务的结果, 当任务不返回任何结果时, 返回值必须是NULL
     */
    @Override
    public Void getRawResult() {
        return null;
    }

    /**
     * 用来设置任务的结果, 当任务不返回任何结果时, 方法为空
     */
    @Override
    protected void setRawResult(Void value) {

    }

    @Override
    protected boolean exec() {
        Date startDate = new Date();
        compute();
        Date finishDate = new Date();
        long diff = finishDate.getTime() - startDate.getTime();
        System.out.printf("MyWorkerTask: %s : %d Milliseconds to complete.\n", name, diff);
        return true;
    }

    protected abstract void compute();

    public String getName() {
        return name;
    }

}
